#!/usr/bin/python2.7
import time
def tpsRelated():
    tps = 5000;

    msWaitTimeout = 100
    maxCntPerRound = tps/1000.0 * msWaitTimeout
    roundPerSecond = 1000/msWaitTimeout
    print "write tps:%d,maxCntPerRound:%d,msWaitTimeout:%d" % (tps, maxCntPerRound, msWaitTimeout)

    print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
    for j in range(10):
        for i in range(roundPerSecond):
            time.sleep(msWaitTimeout/1000.0)
        print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
#end def

from threading import _Timer
class RepeatTimer(_Timer):
    def run(self):
        while not self.finished.is_set():
            self.function(*self.args, **self.kwargs)
            self.finished.wait(self.interval)

class Count:
    def __init__(self):
        self.__count = 0
    def inc(self):
        self.__count = self.__count + 1

    def __str__(self):
        return str(self.__count)

    def get(self):
        return self.__count

import time
import datetime
def timerRun():
    def func(*args, **kwargs):
        cnt = kwargs["cnt"]
        cnt.inc()
        print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) , str(cnt), kwargs["timer"]
        if cnt.get() == 10:
            kwargs["timer"].cancel

        #ts_start = datetime.datetime.now()
        #writeOut(kwargs["maxCntPerRound"])
        #ts_end = datetime.datetime.now()



    count = Count()
    kwargs = {
        "cnt" : count,
    }
    t = RepeatTimer(interval/1000, func, [], kwargs)
    kwargs["timer"] = t

    #t.start()
    #time.sleep(1)
    #t.cancel()

timerRun()



def calcTpsAndInterval():
    tps = 100
    interval = 10 #ms
    maxCntPerRound = tps /1000.0 * interval
    print "tps:%d,interval:%d,maxCntPerRound:%f" %(tps, interval, maxCntPerRound)
    if maxCntPerRound < 1:
        print "Error: please increase the interval"
    #end if
